<?php
namespace App\Repositories\Main;

use App\Models\Main\ComProductModel;
use App\Repositories\BaseRepository;

class AdRepository extends BaseRepository
{
    public function __construct()
    {
        $this->repository = new ComProductModel();
    }

    /**
     * 设置公司、产品关联
     */
    public function setAdd($cid,$cate)
    {
        $model = $this->repository
            ->where('cid',$cid)
            ->where('cate',$cate)
            ->first();
        if (!$model) {
            $dataArr = array(
                'cid' => $cate,
                'cate' => $cate,
                'created_at' => time(),
            );
            $lastId = $this->repository->insertGetId($dataArr);
            return $lastId;
        } else if ($model->del) {
            $this->repository->where('id',$model->id)
                ->update(array(
                    'del' => 0,
                    'updated_at' => time(),
                ));
            $lastId = $model->id;
            return $lastId;
        } else {
            return 0;
        }
    }

    /**
     * 去除关联
     */
    public function setDel($cid,$cate)
    {
        $model = $this->repository
            ->where('cid',$cid)
            ->where('cate',$cate)
            ->where('del',0)
            ->first();
        if (!$model) { return false; }
        $this->repository->where('id',$model->id)
            ->update(array(
                'del' => 1,
                'updated_at' => time(),
            ));
        return true;
    }
}